package com.canieatit.app;

import org.restlet.Response;
import org.restlet.data.Status;
import org.restlet.resource.ClientResource;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.canieatit.gwt.shared.Product;
import com.canieatit.gwt.shared.ProductResource;

public class BarcodeCheckerActivity extends ParentActivity {
	
	private static final String TAG = "BarcodeCheckerActivity";

	// private static final String SERVLET_URL = "http://172.29.80.94:8888/";

	// private final String API_KEY = "AIzaSyAO27nbl_qSqIQ5eHOiG5HBWdgHfLpagP4";
	// private final String SERVLET_URL =
	// "https://www.googleapis.com/shopping/search/v1/public/products?key="+API_KEY+"&country=AU&q=";


	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
//		String barcode = getIntent().getStringExtra(EXTRA_BARCODE_NUMBER);
//		String format = getIntent().getStringExtra(EXTRA_BARCODE_FORMAT);
		
//		ClientResource clientResource = new ClientResource("http://can-i-eat-it.appspot.com/product?barcode=1231231232&format=\"ean\"");
//		ClientResource clientResource = new ClientResource("http://192.168.0.101:8888/product?barcode=1231231232&format=\"ean\"");
		ClientResource clientResource = new ClientResource(RESTLET_TEST_URL);
//		Log.i(TAG, "Request entity: " + clientResource.getRequest().getEntityAsText());
		ProductResource productResource = clientResource.wrap(ProductResource.class);
		Product product = productResource.retrieve();
		Response response = clientResource.getResponse();
		Status status = response.getStatus();
		Log.i(TAG, "Product retrieve status : " + status.getDescription());
		clientResource.release();
		
		
		if (product.productName.equals("Product not found")) {
			Toast.makeText(this, "Product not found. Adding new one", Toast.LENGTH_SHORT).show();
			Intent intent = getIntent();
			intent.setClass(this, AddProductActivity.class);
			startActivity(intent);
			return;
		} else {
			showProduct(product);
		}
	}
	
	private void showProduct(Product product) {
		
		setContentView(R.layout.layout_product);
		
		final TextView productName = (TextView) findViewById(R.id.productName);
		final TextView productSize = (TextView) findViewById(R.id.productSize);
		final TextView productBarcode = (TextView) findViewById(R.id.productBarcode);
		final TextView productCountry = (TextView) findViewById(R.id.productCountry);
		final TextView isHalalCertifed = (TextView) findViewById(R.id.isHalalCertified);
		
		productName.setText(product.productName);
		productSize.setText(product.productSize);
		productCountry.setText(product.productCountry);
		productBarcode.setText(product.barCode);
		
		Log.i(TAG, "Product contributor : " + product.contributorEmail);
		
		if (product.reviewedByAdmin) {
			isHalalCertifed.setText(product.halalCertification);
		} else {
			isHalalCertifed.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
			isHalalCertifed.setText("Under review by admin");
		}
		
		
		final ImageView productImage = (ImageView) findViewById(R.id.productImage);
		BarcoderApplication app = (BarcoderApplication) getApplication();
		app.getImageLoader().DisplayImage(product.imageUrl, productImage);
		
	}


}
